<?php
	session_start();
	/**
	*@package ParkFind
	*@desc Page for deleting user from the database.
	*/
	/**
	*@desc Deletes the user from the database
	*@param int $user_id
	*/
	function deleteuser($user_id)
	{
		global $connection;
		
		//prepare queries
		$deleteuser = "DELETE FROM users WHERE user_id = $user_id";
		$deletecomments = "DELETE FROM comments WHERE user_id = $user_id";
		$deletereports = "DELETE FROM reports WHERE user_id = $user_id";
		$deleteratings = "DELETE FROM ratings WHERE user_id = $user_id";
		$deleteproposals = "DELETE FROM reports WHERE user_id = $user_id";
		
		//run queries
		if (!$result = mysql_query ($deletecomments, $connection))
			die("Failed to delete user comments");
		if (!$result = mysql_query ($deletereports, $connection))
			die("Failed to delete user reports");
		if (!$result = mysql_query ($deleteratings, $connection))
			die("Failed to delete user ratings");
		if (!$result = mysql_query ($deleteproposals, $connection))
			die("Failed to delete user proposals");
		if (!$result = mysql_query ($deleteuser, $connection))
			die("Failed to delete USER");
	}
	
	// Direct calling check
	if (!isset($_POST["submit"]))
		die("Do not call this page directly");
	
	
	if($_POST["submit"] == "Cancel")
	{
		header("Location: profile.php");
		exit();
	}
	
	 
	// Get variable
	$user_id = $_SESSION["user_id"];

	/**
	* @desc This include file connects to the database.
	*/
	require 'includes/connect.inc';

	//check if user exists
	$user = "SELECT user_id FROM users WHERE user_id = $user_id";

	//run query
	if (!$result = mysql_query ($user, $connection))
		die("Could not run query");
		
	if(mysql_num_rows($result) < 1)
		die("Could not find user_id");		

	deleteuser($user_id);
	
	session_destroy();

	mysql_close($connection);

	header("Location: profiledeleted.php");
	exit();
	
?>